<ix-modal-header
  [requiredRoles]="requiredRoles"
  [title]="title"
  [loading]="isFormLoading()"
></ix-modal-header>

<form class="ix-form-container" [formGroup]="form" (submit)="onSubmit()">
  <ix-fieldset [title]="'Identification' | translate">
    <div class="columns">
      <div>
        <ix-input
          formControlName="full_name"
          [label]="'Full Name' | translate"
          [required]="true"
        ></ix-input>

        <ix-input
          formControlName="username"
          [label]="'Username' | translate"
          [tooltip]="tooltips.username | translate"
          [required]="true"
        ></ix-input>

        <ix-input
          formControlName="email"
          [label]="'Email' | translate"
        ></ix-input>
      </div>

      <div>
        @if (isStigMode() && isNewUser) {
          <ix-radio-group
            formControlName="stig_password"
            [label]="'Password' | translate"
            [required]="true"
            [options]="stigPasswordOptions$"
          ></ix-radio-group>
        } @else {
          <ix-slide-toggle
            formControlName="password_disabled"
            [label]="'Disable Password' | translate"
            [tooltip]="tooltips.password_disabled | translate"
          ></ix-slide-toggle>

          <ix-input
            formControlName="password"
            type="password"
            [label]="isNewUser ? ('Password' | translate) : ('New Password' | translate)"
            [tooltip]="(isNewUser ? tooltips.password : tooltips.password_edit) | translate"
            [required]="isNewUser"
          ></ix-input>

          <ix-input
            formControlName="password_conf"
            type="password"
            [label]="isNewUser ? ('Confirm Password' | translate) : ('Confirm New Password' | translate)"
            [required]="isNewUser"
          ></ix-input>
        }
      </div>
    </div>
  </ix-fieldset>

  <mat-divider></mat-divider>

  <ix-fieldset [title]="'User ID and Groups' | translate">
    <div class="columns">
      <div>
        <ix-input
          formControlName="uid"
          type="number"
          [label]="'UID' | translate"
          [tooltip]="tooltips.uid | translate"
          [required]="true"
        ></ix-input>

        <ix-slide-toggle
          formControlName="group_create"
          [label]="'Create New Primary Group' | translate"
          [tooltip]="tooltips.group_create | translate"
        ></ix-slide-toggle>
      </div>

      <div>
        <ix-chips
          formControlName="groups"
          [label]="'Auxiliary Groups' | translate"
          [tooltip]="tooltips.groups | translate"
          [resolveValue]="true"
          [resolveOptions]="groupOptions$"
          [autocompleteProvider]="autocompleteProvider"
        ></ix-chips>

        <ix-combobox
          formControlName="group"
          [label]="'Primary Group' | translate"
          [tooltip]="tooltips.group | translate"
          [provider]="groupProvider"
        ></ix-combobox>
      </div>
    </div>
  </ix-fieldset>

  <mat-divider></mat-divider>

  <div class="columns">
    <div>
      <ix-fieldset [title]="'Directories and Permissions' | translate">
        <ix-explorer
          formControlName="home"
          [label]="'Home Directory' | translate"
          [tooltip]="tooltips.home | translate"
          [nodeProvider]="treeNodeProvider"
          [canCreateDataset]="!editingUser"
        ></ix-explorer>

        <ix-permissions
          formControlName="home_mode"
          [label]="'Home Directory Permissions' | translate"
          [tooltip]="tooltips.home_mode | translate"
        ></ix-permissions>

        <ix-checkbox
          formControlName="home_create"
          [label]="'Create Home Directory' | translate"
        ></ix-checkbox>
      </ix-fieldset>
    </div>

    <div>
      <ix-fieldset [title]="'Authentication' | translate">
        <ix-textarea
          formControlName="sshpubkey"
          [label]="'Authorized Keys' | translate"
          [tooltip]="tooltips.sshpubkey | translate"
        ></ix-textarea>

        <ix-file-input
          formControlName="sshpubkey_file"
          [label]="'Upload SSH Key' | translate"
        ></ix-file-input>

        <ix-checkbox
          formControlName="ssh_password_enabled"
          [label]="'SSH password login enabled' | translate"
        ></ix-checkbox>

        <ix-select
          formControlName="shell"
          [label]="'Shell' | translate"
          [tooltip]="tooltips.shell | translate"
          [options]="shellOptions$"
          [required]="true"
        ></ix-select>

        <ix-checkbox
          formControlName="locked"
          [label]="'Lock User' | translate"
          [tooltip]="tooltips.locked | translate"
        ></ix-checkbox>

        <ix-chips
          formControlName="sudo_commands"
          [label]="'Allowed sudo commands' | translate"
        ></ix-chips>

        <ix-checkbox
          formControlName="sudo_commands_all"
          [label]="'Allow all sudo commands' | translate"
        ></ix-checkbox>

        <ix-chips
          formControlName="sudo_commands_nopasswd"
          [label]="'Allowed sudo commands with no password' | translate"
        ></ix-chips>

        <ix-checkbox
          formControlName="sudo_commands_nopasswd_all"
          [label]="'Allow all sudo commands with no password' | translate"
        ></ix-checkbox>

        <ix-checkbox
          formControlName="smb"
          [label]="'SMB User' | translate"
          [tooltip]="smbTooltip"
        ></ix-checkbox>
      </ix-fieldset>
    </div>
  </div>

  <ix-form-actions>
    <button
      *ixRequiresRoles="requiredRoles"
      mat-button
      type="submit"
      color="primary"
      ixTest="save"
      [disabled]="form.invalid || isFormLoading()"
    >
      {{ 'Save' | translate }}
    </button>
    @if (!isNewUser) {
      <button
        mat-button
        type="button"
        ixTest="download-keys"
        (click)="onDownloadSshPublicKey()"
      >
        {{ 'Download Authorized Keys' | translate }}
      </button>
    }
  </ix-form-actions>
</form>
